﻿using Face8280WebSocketServer.Command;
using DoNetDrive.Core.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DoNetDrive.Core.Packet;
using Newtonsoft.Json.Serialization;

namespace Face8280WebSocketServer.Command.Record
{
    public class GetRecord : JsonCommand
    {

        int totalCount;

        //    Record_DTO RecordDTO = new Record_DTO();

      //  Models.DoorSensorRecord Record;
        List<Models.DoorSensorRecord> RecordList = new List<Models.DoorSensorRecord>();
        public GetRecord(INCommandDetail cd, Models.RecordType type, int readSize = 100) : base(cd, null)
        {
            if (readSize > 100) readSize = 100;
            mSendObject = new { cmd = Reply, recordType = type.ToString(), readSize };
        }
        protected override void CommandNext(INPacket readPacket)
        {
            if (readPacket is JsonPacket)
            {
                var jPck = readPacket as JsonPacket;
                var sJson = jPck.GetJson();
                if (CheckJsonResult(sJson) == false)
                {
                    return;
                }
                var data = sJson.FromJson<Record_DTO>();
                totalCount = data.Total;

                switch (data.RecordType)
                {
                    case Models.RecordType.Enter:
                        var cardRecord = data.Records.ToString().FromJson<Models.CardRecord>();
                        RecordList.Add(cardRecord);
                        cardRecord.File = jPck.GetBinaryData();
                        break;
                    case Models.RecordType.Alarm:
                        var alarmRecords = data.Records.ToString().FromJson<List<Models.AlarmRecord>>();
                        RecordList.AddRange(alarmRecords);
                        break;
                    case Models.RecordType.Door:
                        var doorSensors = data.Records.ToString().FromJson<List<Models.DoorSensorRecord>>();
                        RecordList.AddRange(doorSensors);
                        break;
                    case Models.RecordType.System:
                        var systemRecords = data.Records.ToString().FromJson<List<Models.SystemRecord>>();
                        RecordList.AddRange(systemRecords);
                        break;
                    default:
                        break;
                }
                if (totalCount == 0)
                {
                    _Result = new GetRecordResult(data.RecordType, RecordList);
                    CommandCompleted();
                    return;
                }
                CommandWaitResponse();
            }
        }
    }
}
